import { createRouter, createWebHistory } from 'vue-router'
import Layout from '../layout/Layout.vue'
import {Comment} from "@element-plus/icons";

const routes = [

  {
    path: '/',
    component:Layout,
    redirect: '/',
    meta: { title: '首页' },
    children: [
      {
        path: '/',
        name: 'home',
        component: () => import("@/views/Home"),
        meta: { title: '' }
      },

      {
        path: 'setting',
        name: 'setting',
        component: () => import("@/views/Setting"),
        // redirect: "/setting/user",
        // 路由元数据
        meta: {
          title: '系统管理'
        },

        children: [
          {
            path: "user",
            name: "User",
            component: () => import("@/views/User"),
            meta: {
              title: '用户管理'
            },

          } ,
          {
            path: 'person',
            name: 'Person',
            component: () => import("@/views/Person"),
            mata: {
              title: '个人管理'
            }
          },
          {
            path: "book",
            name: "Book",
            component: () => import("@/views/Book"),

            meta: {
              title: '书籍管理'
            }

          },
          {
            path: 'news',
            name: 'News',
            component: () => import("@/views/News"),
            meta: {
              title: '新闻管理'
            }
          },
          {
            path: 'category',
            name: 'Category',
            component: () => import("@/views/Category"),
            meta: {
              title: '分类管理'
            }
          },

          {
            path: 'map',
            name: 'Map',
            component: () => import("@/views/Map"),
            meta: {
              title: '百度地图'
            }
          },

          {
            path: 'im',
            name: 'Im',
            component: () => import("@/views/Im"),
            meta: {
              title: '聊天室'
            }
          },
          {
            path: 'message',
            name: 'Message',
            component: () => import("@/views/Message"),
            meta: {
              title: '在线留言'
            }
          },
          {
            path: 'donate',
            name: 'Donate',
            component: () => import("@/views/Donate"),
            meta: {
              title: '喝咖啡'
            }
          },
          {
            path: 'order',
            name: 'Order',
            component: () => import("@/views/Order"),
            meta: {
              title: '订单'
            }
          }



        ]
      },
    ]
  },



  {
    path: '/Login',
    name: 'Login',
    component: () =>import("@/views/Login")
  } ,
  {
    path: '/register',
    name: 'register',
    component: () =>import("@/views/Register")
  }

]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 限制某些页面禁止未登录访问**************************
let limitPagePath = ['/user']
//前置路由守卫

router.beforeEach((to, from, next) => {
  if (limitPagePath.includes(to.path)) {
    // 判断sessionStorage是否保存了用户信息
    let userStr = sessionStorage.getItem("user") || "{}"
    let user = JSON.parse(userStr)
    if (!user.id) {
      // 跳转到登录页面
      next({path: "/login"})
    } else {
      //放行
      next()
    }
  } else {
    next()
  }
})
export default router
